var app = getApp();
//项目URL相同部分，减轻代码量，同时方便项目迁移
//这里因为我是本地调试，所以host不规范，实际上应该是你备案的域名信息
// var host = 'http://localhost:8085/';
var host = "http://localhost:8085/";

/**
 * POST请求，
 * URL：接口
 * postData：参数，json类型
 * doSuccess：成功的回调函数
 * doFail：失败的回调函数
 */



function request(url, postData, doSuccess, doFail) {
    let make = "";
    make = wx.getStorageSync('tokenID');
    //    wx.setStorageSync('tokenID', tokenID);
    wx.request({
        //项目的真正接口，通过字符串拼接方式实现
        url: host + url,
        header: {
            "content-type": "application/json;charset=UTF-8",
            "x-auth-token": make,
        },
        data: postData,
        method: 'POST',
        success: function(res) {
            //参数值为res.data,直接将返回的数据传入
            //   wx.setStorageSync('mm', uu);
            doSuccess(res.data);
        },
        fail: function() {
            doFail();
        },
    })
}

function request2(url, postData, doSuccess, doFail) {
    // let make = "";
    // make = wx.getStorageSync('tokenID');
    // //    wx.setStorageSync('tokenID', tokenID);
    wx.request({
        //项目的真正接口，通过字符串拼接方式实现
        url: host + url,
        header: {
            "content-type": "application/json;charset=UTF-8",
        },
        data: postData,
        method: 'POST',
        success: function(res) {
            //参数值为res.data,直接将返回的数据传入
            //   wx.setStorageSync('mm', uu);
            doSuccess(res.data);
        },
        fail: function() {
            doFail();
        },
    })
}

function request3(url, postData, doSuccess, doFail) {
    // let make = "";
    // make = wx.getStorageSync('tokenID');
    // //    wx.setStorageSync('tokenID', tokenID);
    wx.request({
        //项目的真正接口，通过字符串拼接方式实现
        url: host + url,
        header: {
            "content-type": "application/json;charset=UTF-8",
        },
        data: postData,
        method: 'POST',
        success: function(res) {
            //参数值为res.data,直接将返回的数据传入
            //   wx.setStorageSync('mm', uu);
            doSuccess(res);
        },
        fail: function() {
            doFail();
        },
    })
}

/**
 * POST请求，
 * URL：接口
 * postData：参数，json类型
 * doSuccess：成功的回调函数
 * doFail：失败的回调函数
 */
function request1(url, postData, doSuccess, doFail) {
    //此处获取并设置请求头的token
    let make = "";
    make = wx.getStorageSync('tokenID');
    //    wx.setStorageSync('tokenID', tokenID);
    wx.request({
        //项目的真正接口，通过字符串拼接方式实现
        url: host + url,
        header: {
            "content-type": "application/json;charset=UTF-8",
            "x-auth-token": make,
        },
        data: postData,
        method: 'POST',
        success: function(res) {
            //参数值为res.data,直接将返回的数据传入
            //此处获取并设置本地的token
            // console.log(res.header["x-auth-token"]);
            // let tokenID = res.header["x-auth-token"];
            // if (tokenID != undefined && tokenID != "") {
            //     wx.setStorageSync('tokenID', tokenID);
            // }
            doSuccess(res.data);
        },
        fail: function() {
            doFail();
        },
    })
}


/**
 * POST请求，
 * URL：接口
 * postData：参数，json类型
 * doSuccess：成功的回调函数
 * doFail：失败的回调函数
 */
function request4(url, postData, doSuccess, doFail) {
    //此处获取并设置请求头的token
    let make = "";
    make = wx.getStorageSync('tokenID');
    //    wx.setStorageSync('tokenID', tokenID);
    wx.request({
        //项目的真正接口，通过字符串拼接方式实现
        url: host + url,
        header: {
            "content-type": "application/json;charset=UTF-8",
            "x-auth-token": make,
        },
        data: JSON.stringify(postData),
        method: 'PUT',
        success: function(res) {
            //参数值为res.data,直接将返回的数据传入
            //此处获取并设置本地的token
            // console.log(res.header["x-auth-token"]);
            // let tokenID = res.header["x-auth-token"];
            // if (tokenID != undefined && tokenID != "") {
            //     wx.setStorageSync('tokenID', tokenID);
            // }
            doSuccess(res.data);
        },
        fail: function() {
            doFail();
        },
    })
}

//GET请求，不需传参，直接URL调用，
function getData(url, doSuccess, doFail) {
    wx.request({
        url: host + url,
        header: {
            "content-type": "application/json;charset=UTF-8"
        },
        method: 'GET',
        success: function(res) {
            console.log(res);
            doSuccess(res.data);
        },
        fail: function() {
            doFail();
        },
    })
}

let ak = 0;
//文件上传，带token
//分别是接口地址，文件地址，后端图片接收参数，成功回调，失败回调,
function upFile(url, fileUrl, name, doSuccess, doFail, index, pushData, pushURL) {
    // let fileName2 = "";
    // fileName2 = wx.getStorageSync('fileName');
    // console.log(fileName2)
    // if(index>=1){
    //   url = url.replace(/null/g,fileName2)
    // }
    // let nowThis=this;
    // let make = "";
    // make = wx.getStorageSync('tokenID');
    // console.log(make)
    // let fileUrltest=fileUrl[index];
    // wx.uploadFile({
    //   url: url,
    //   filePath: fileUrltest,
    //   header: {
    //     'Content-Type': 'multipart/form-data',
    //     "x-auth-token": make
    //   },
    //   async: false,
    //   name: name,
    //   formData: {
    //     method: 'POST'   //请求方式
    //   },
    //   success: function (res) {
    //     let resData = JSON.parse(res.data);
    //     console.log(res);
    //     // let tokenID = res.header["x-auth-token"];
    //     // console.log(tokenID)
    //     // if(tokenID!=undefined&&tokenID!=""){
    //     //   wx.setStorageSync('tokenID', tokenID);
    //     // }

    //     let fileName = resData.date[0].fileName;
    //     if(fileName!=undefined&&fileName!=""){
    //       wx.setStorageSync('fileName', fileName);
    //     }
    //     let fileName1 = "";
    //     fileName1 = wx.getStorageSync('fileName');
    //     console.log(fileName1)

    //     doSuccess(resData);
    //     if(fileUrl.length>1&&index<fileUrl.length){
    //       ak = ak +1;
    //       // let j = 0;
    //       console.log(ak)
    //      for (let j = index+1; j < fileUrl.length; ++j) {
    //       nowThis.upFile(url,fileUrl,name,function (res){
    //         console.log(res.date);
    //       },function (e){
    //         console.log(e)
    //       },j,pushData,pushURL)
    //      }

    //     }

    //     if(index==fileUrl.length-1){
    //       console.log(index)
    //       request1(pushURL, pushData, function(res) {
    //         console.log(res)
    //     }, function(e) {

    //     })
    //     }
    //   },
    //   fail: function () {
    //     doFail();
    //   }
    // })
}


// let data = {
//   url: url,//这里是你要上传的服务器接口
//   path: kk,//这里是你最开始定义的图片数组
//   data:{
//     pushURL:url,
// pushData: pushData,
// pushDataChange: function (pushData,FileName) {
//     console.log(FileName);
//     pushData.pet_picture = FileName;
//     return pushData
// },//这是个传参函数,用于改变pushData需要的数据
//     success: function (res){
//       console.log(res)
//     },
//     fail: function (e){
//       console.log(e)
//     }
//   }
// }

//带token上传图片，参数：url+图片url，发布数据，发布url，发布成功回调，
//失败回调   上面data，data2是案例
function uploadImage(data) {
    //图片小于一张就无法执行
    if (data.path.length > 0) {
        let make = "";
        make = wx.getStorageSync('tokenID');
        var that = this,
            i = data.i ? data.i : 0,
            success = data.success ? data.success : 0,
            fail = data.fail ? data.fail : 0;
        // let fileName = "";
        // fileName = wx.getStorageSync('fileName');
        // if(i > 0&&fileName!=undefined){
        //   data.url = data.url.replace(/null/g,fileName);
        //   data.data.pushData.fileName = fileName;
        // }
        // console.log(fileName)
        wx.uploadFile({
            url: host + data.url,
            filePath: data.path[i],
            header: {
                'Content-Type': 'multipart/form-data',
                "x-auth-token": make
            },
            name: 'img',
            formData: {
                method: "POST"
            },
            success: (resp) => {
                let res = JSON.parse(resp.data);
                console.log(res)
                let fileName = res.date[0].fileName;
                console.log(i);
                if (fileName != undefined && fileName != "" && i == 0) {
                    // wx.setStorageSync('fileName', fileName);
                    // console.log(fileName)
                    console.log(data.data);
                    // if(i > 0&&fileName!=undefined){

                    //   }
                    if (data.data.pushDataChange != "" && data.data.pushDataChange != undefined) {
                        data.url = data.url.replace(/null/g, fileName);
                        data.data.pushData = data.data.pushDataChange(data.data.pushData, fileName);
                        console.log("执行了");
                    } else {
                        data.url = data.url.replace(/null/g, fileName);
                    }
                }
                success++;
                console.log(resp)
                console.log(i);
            },
            fail: (res) => {
                fail++;
                console.log('fail' + i + "fail:" + fail);
            },
            complete: () => {
                console.log(i);
                i++;
                if (i == data.path.length) { //图片上传完，停止调用
                    //停止调用后进行上传信息
                    // console.log(data.data);
                    // console.log(data)
                    // if(data.data.pushData1!=""&&data.data.pushData1!=undefined){
                    //     data.data.pushData = data.data.pushData1(data.data.pushData);
                    //     console.log("执行了");
                    // }
                    // console.log(data.data.pushData);
                    request1(data.data.pushURL, data.data.pushData, function(res) {
                        data.data.success(res);
                    }, function(e) {
                        data.data.fail(e);
                    })
                    console.log('执行完毕');
                    console.log('成功：' + success + "失败：" + fail);
                } else { //图片还没上传完，继续调用
                    console.log(i);
                    data.i = i;
                    data.success = success;
                    data.fail = fail;
                    that.uploadImage(data);
                }
            }
        })

    }
}

//修改专用的图片上传
function upImg(data) {
    let make = "";
    make = wx.getStorageSync('tokenID');
    wx.uploadFile({
        url: host + data.URL,
        filePath: data.path,
        header: {
            'Content-Type': 'multipart/form-data',
            "x-auth-token": make
        },
        name: 'img',
        formData: {
            method: "POST"
        },
        success: (res) => {
            res.data = JSON.parse(res.data);
            data.success(res.data);
        },
        fail: (resp) => {
            data.fail(resp);
        }
    })
}

//Delete请求，带token,修改专用
function deleteImg(deleteData) {
    let make = "";
    make = wx.getStorageSync('tokenID');
    wx.request({
        url: host + deleteData.URL,
        header: {
            "content-type": "application/json;charset=UTF-8",
            "x-auth-token": make
        },
        method: 'DELETE',
        success: function(res) {
            deleteData.success(res.data);
        },
        fail: function(resp) {
            deleteData.fail(resp);
        },
    })
}


//GET请求，带token
function getData1(url, doSuccess, doFail) {
    let make = "";
    make = wx.getStorageSync('tokenID');
    console.log(make)
    wx.request({
        url: host + url,
        header: {
            "content-type": "application/json;charset=UTF-8",
            "x-auth-token": make
        },
        method: 'GET',
        success: function(res) {
            // console.log(res.header["x-auth-token"]);
            // let tokenID = res.header["x-auth-token"];
            // if (tokenID != undefined && tokenID != "") {
            //     wx.setStorageSync('tokenID', tokenID);
            // }
            doSuccess(res.data);
        },
        fail: function() {
            doFail();
        },
    })
}


//put方式
function passRequest(url, doSuccess, doFail) {
    let make = "";
    make = wx.getStorageSync('tokenID');
    console.log(make)
    wx.request({
        url: host + url,
        header: {
            "content-type": "application/json;charset=UTF-8",
            "x-auth-token": make
        },
        method: 'PUT',
        success: function(res) {
            // console.log(res.header["x-auth-token"]);
            // let tokenID = res.header["x-auth-token"];
            // if (tokenID != undefined && tokenID != "") {
            //     wx.setStorageSync('tokenID', tokenID);
            // }
            doSuccess(res.data);
        },
        fail: function() {
            doFail();
        },
    })
}

function up(url, path, doSuccess, doFail) {
    //此处获取并设置请求头的token
    let make = "";
    make = wx.getStorageSync('tokenID');
    wx.uploadFile({
        url: host + url,
        filePath: path,
        header: {
            'Content-Type': 'multipart/form-data',
            "x-auth-token": make
        },
        name: 'file',
        formData: {
            method: "POST"
        },
        success: function(res) {
            doSuccess(res.data);
        },
        fail: function() {
            doFail();
        },
    })
}





function passRequest1(url, postData, doSuccess, doFail) {
    //此处获取并设置请求头的token
    let make = "";
    make = wx.getStorageSync('tokenID');
    //    wx.setStorageSync('tokenID', tokenID);
    wx.request({
        //项目的真正接口，通过字符串拼接方式实现
        url: host + url,
        header: {
            "content-type": "application/json;charset=UTF-8",
            "x-auth-token": make,
        },
        data: postData,
        method: 'PUT',
        success: function(res) {
            //参数值为res.data,直接将返回的数据传入
            //此处获取并设置本地的token
            // console.log(res.header["x-auth-token"]);
            // let tokenID = res.header["x-auth-token"];
            // if (tokenID != undefined && tokenID != "") {
            //     wx.setStorageSync('tokenID', tokenID);
            // }
            doSuccess(res.data);
        },
        fail: function() {
            doFail();
        },
    })
}

/**
 * module.exports用来导出代码
 * js文件中通过var call = require("../../utils/request.js")  加载
 * 在引入引入文件的时候"  "里面的内容通过../../../这种类型，小程序的编译器会自动提示，因为你可能
 * 项目目录不止一级，不同的js文件对应的工具类的位置不一样
 */
module.exports.request = request;
module.exports.request1 = request1;
module.exports.getData = getData;
module.exports.getData1 = getData1;
module.exports.upFile = upFile;
module.exports.uploadImage = uploadImage;
module.exports.request2 = request2;
module.exports.request3 = request3;
module.exports.passRequest = passRequest;
module.exports.passRequest1 = passRequest1;
module.exports.up = up;
module.exports.upImg = upImg;
module.exports.deleteImg = deleteImg;
module.exports.request4 = request4;